package org.youyun56.gpsserver.command;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.xml.sax.SAXException;
import org.youyun56.gpsserver.ServerStatus;
import org.youyun56.gpsserver.ServiceType;
import org.youyun56.gpsserver.command.console.SocketCommand;
import org.youyun56.gpsserver.common.MainEngine;
import org.youyun56.gpsserver.common.PathConfig;

public abstract class AbstractService {
    private final static Logger log = Logger.getLogger(AbstractService.class);
    protected BufferedReader br;
    protected ServerStatus status;
    protected ServiceType serviceType;

    public void shutdown() {
        this.status = ServerStatus.down;
        MainEngine.instance.getServer().shutdown();
    }

    public void loadCommands() throws ParserConfigurationException, SAXException, IOException {
        CommandConfigLoader loader = new CommandConfigLoader();
        loader.loadAllConfig(PathConfig.getCommandFolder());
    }

    public void run() throws Exception {
        status = ServerStatus.active;
        init();
    }

    protected void init() throws ParserConfigurationException, SAXException, IOException {
        configLog4j();
        addShutDownHook();
        startConsolerListener();
        loadCommands();
    }

    private void configLog4j() {
        System.setProperty("server", serviceType.name().toLowerCase());
        DOMConfigurator.configure(PathConfig.ROOT + "/log4j.xml");
    }

    protected void startConsolerListener() {
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                br = new BufferedReader(new InputStreamReader(System.in));
                try {
                    String command = null;
                    while ((command = br.readLine().trim()) != null) {
                        SocketCommand sc = new SocketCommand(command);
                        System.out.println(CommandAssigner.getInstance().assign(sc));
                    }
                }
                catch (IOException e) {
                    log.error("consoler reader is interrupted!", e);
                }
                finally {
                    try {
                        br.close();
                    }
                    catch (IOException e) {
                        // TODO Auto-generated catch block
                        // e.printStackTrace();
                    }
                }
            }
        });
        thread.setDaemon(true);
        thread.start();
    }

    protected void addShutDownHook() {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                log.info("dd");
                shutdown();
            }
        });
    }

    public ServiceType getServiceType() {
        return serviceType;
    }

    public void setServiceType(ServiceType serviceType) {
        this.serviceType = serviceType;
    }

}
